package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.BaoDanAdapter;
import com.lc.afbx.conn.GetApiMyOrder;
import com.lc.afbx.conn.GetApiMyOrderEdit;
import com.lc.afbx.dialog.AlertDialog;
import com.lc.afbx.interf.BaoDanIdInterf;
import com.lc.afbx.util.DateTimePickDialogUtil;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.TimeUtil;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class PolicyMineActivity extends BaseActivity implements View.OnClickListener, BaoDanIdInterf {


    private TextView policy_mine_endtime_tv;
    private TextView policy_mine_starttime_tv;
    private RelativeLayout policy_mine_starttime_rl;
    private RelativeLayout policy_mine_endtime_rl;
    private Button policy_mine_search_bt;
    private String initDateTime;
    private String initEndTime;

    private ListView policy_mine_lv;
    private LinearLayout policy_mine_title_tab_ll;


    private BaoDanAdapter adapter;
    private List lists = new ArrayList();

    //两个textView  车险  寿险
    private TextView policy_mine_tab_car_tv;
    private TextView policy_mine_tab_life_tv;

    private String start;
    private String end;
    private TextView policy_mine_tab_car_line_tv;
    private TextView policy_mine_tab_life_line_tv;

    private Boolean isMulChoice = false;
    private TextView policy_mine_edit_tv;
    private int i;//这个i是编辑事件判断用的


    private GetApiMyOrder getApiMyOrder = new GetApiMyOrder(new AsyCallBack<GetApiMyOrder.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiMyOrder.Info info) throws Exception {

            lists = info.data.businessList;

            policy_mine_lv.setAdapter(adapter = new BaoDanAdapter(context, lists, isMulChoice));

            if (lists.size() > 0) {

                policy_mine_edit_tv.setVisibility(View.VISIBLE);

            } else {

                policy_mine_edit_tv.setVisibility(View.GONE);

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }
    });


    private GetApiMyOrderEdit getApiMyOrderEdit = new GetApiMyOrderEdit(new AsyCallBack<GetApiMyOrderEdit.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiMyOrderEdit.Info info) throws Exception {

            if (info.code.equals("200")) {
                UtilToast.show(context, "删除成功");

                policy_mine_edit_tv.setText("编辑");

                if ((Boolean) policy_mine_tab_car_tv.getTag()) {
                    policy_mine_tab_car_tv.performClick();
                } else {
                    policy_mine_tab_life_tv.performClick();
                }

            } else {
                i = 1;      //这让i = 1  点击删除走  else
                UtilToast.show(context, "删除失败");
            }
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            i = 1;
            MyToast.showToast("网络不好，请重试", context);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy_mine);

        Calendar calendar = Calendar.getInstance();
        //dialog显示当前时间
        initDateTime = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) - 3) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";
        initEndTime = calendar.get(Calendar.YEAR) + "年" + calendar.get(Calendar.MONTH) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";

        policy_mine_endtime_tv = ((TextView) findViewById(R.id.policy_mine_endtime_tv));

        policy_mine_starttime_tv = ((TextView) findViewById(R.id.policy_mine_starttime_tv));

        //布局  添加点击监听
        policy_mine_starttime_rl = ((RelativeLayout) findViewById(R.id.policy_mine_starttime_rl));
        policy_mine_endtime_rl = ((RelativeLayout) findViewById(R.id.policy_mine_endtime_rl));
        policy_mine_search_bt = ((Button) findViewById(R.id.policy_mine_search_bt));

        policy_mine_lv = ((ListView) findViewById(R.id.policy_mine_lv));

        policy_mine_title_tab_ll = ((LinearLayout) findViewById(R.id.policy_mine_title_tab_ll));

        //tab
        policy_mine_tab_car_tv = ((TextView) findViewById(R.id.policy_mine_tab_car_tv));
        policy_mine_tab_life_tv = ((TextView) findViewById(R.id.policy_mine_tab_life_tv));
        //tabline
        policy_mine_tab_life_line_tv = ((TextView) findViewById(R.id.policy_mine_tab_life_line_tv));
        policy_mine_tab_car_line_tv = ((TextView) findViewById(R.id.policy_mine_tab_car_line_tv));

        //编辑按钮
        policy_mine_edit_tv = ((TextView) findViewById(R.id.policy_mine_edit_tv));

        setListener();


    }

    private void setListener() {

        policy_mine_starttime_rl.setOnClickListener(this);
        policy_mine_endtime_rl.setOnClickListener(this);
        policy_mine_search_bt.setOnClickListener(this);
        policy_mine_tab_car_tv.setOnClickListener(this);
        policy_mine_tab_life_tv.setOnClickListener(this);

        policy_mine_edit_tv.setOnClickListener(this);

        policy_mine_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = null;
                if (lists.get(position) instanceof GetApiMyOrder.Business) {

                    intent = new Intent(context, PolicyDetailsCarActivity.class);
                    GetApiMyOrder.Business business = (GetApiMyOrder.Business) lists.get(position);
                    intent.putExtra("id", business.id);
                    intent.putExtra("type", business.type);

                } else if (lists.get(position) instanceof GetApiMyOrder.Accident) {

                    intent = new Intent(context, PolicyDetailsCasualtyActivity.class);
                    GetApiMyOrder.Accident accident = (GetApiMyOrder.Accident) lists.get(position);
                    intent.putExtra("id", accident.id);
                    intent.putExtra("type", accident.type);

                } else {
                    UtilToast.show(context, "dataList没数据咋地");
                }

                startActivity(intent);

            }
        });

    }




    @Override
    public void onClick(View view) {

        DateTimePickDialogUtil dialogUtil = null;

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.policy_mine_edit_tv:

                if (++i % 2 != 0) {
                    policy_mine_edit_tv.setText("删除");
                    adapter = new BaoDanAdapter(context, lists, true);
                    policy_mine_lv.setAdapter(adapter);
                } else {

                    adapter.doCallback(); //这是删除的回调  adapter里写的方法

                }
                return;

            case R.id.policy_mine_starttime_rl:
                dialogUtil = new DateTimePickDialogUtil(this, initDateTime);
                dialogUtil.dateTimePicKDialog(policy_mine_starttime_tv);
                return;

            case R.id.policy_mine_endtime_rl:
                dialogUtil = new DateTimePickDialogUtil(this, initEndTime);
                dialogUtil.dateTimePicKDialog(policy_mine_endtime_tv);
                return;

            case R.id.policy_mine_search_bt:
                //点击查询的时候 就把编辑状态还原
                policy_mine_edit_tv.setText("编辑");
                i = 0;
                //区分tab哪个被选中了
                policy_mine_tab_car_tv.setTag(true);
                policy_mine_tab_life_tv.setTag(false);

                if (policy_mine_starttime_tv.getText().toString().isEmpty() || policy_mine_endtime_tv.getText().toString().isEmpty()) {
                    UtilToast.show(context, "请选择时间");
                    return;
                }
                start = TimeUtil.dataOne(policy_mine_starttime_tv.getText().toString());
                end = TimeUtil.dataOne(policy_mine_endtime_tv.getText().toString());

                if (Long.valueOf(end) - Long.valueOf(start) >= 0) {

                    policy_mine_tab_car_tv.setTextColor(getResources().getColor(R.color.baodanTab));
                    policy_mine_tab_life_tv.setTextColor(getResources().getColor(R.color.textcolorHei));
                    policy_mine_title_tab_ll.setVisibility(View.VISIBLE);
                    //tab底部指示条
                    policy_mine_tab_life_line_tv.setVisibility(View.GONE);
                    policy_mine_tab_car_line_tv.setVisibility(View.VISIBLE);

                    getApiMyOrder.uid = BaseApplication.BasePreferences.readUserId();
                    getApiMyOrder.type = PrefrenceConfig.ONE;
                    getApiMyOrder.starttime = start;
                    getApiMyOrder.endtime = end;

                    getApiMyOrder.execute(this);

                    return;
                } else {
                    UtilToast.show(context, "起始时间 大于 结束时间");
                    return;
                }

            case R.id.policy_mine_tab_car_tv:

                //把编辑状态还原
                policy_mine_edit_tv.setText("编辑");
                i = 0;
                //设置标记  区分哪个tab被选择了
                policy_mine_tab_car_tv.setTag(true);
                policy_mine_tab_life_tv.setTag(false);

                policy_mine_tab_life_line_tv.setVisibility(View.GONE);
                policy_mine_tab_car_line_tv.setVisibility(View.VISIBLE);

                policy_mine_tab_car_tv.setTextColor(getResources().getColor(R.color.baodanTab));
                policy_mine_tab_life_tv.setTextColor(getResources().getColor(R.color.textcolorHei));

                getApiMyOrder.uid = BaseApplication.BasePreferences.readUserId();
                getApiMyOrder.type = PrefrenceConfig.ONE;
                getApiMyOrder.starttime = start;
                getApiMyOrder.endtime = end;

                getApiMyOrder.execute(this);

                return;

            case R.id.policy_mine_tab_life_tv:

                //把编辑状态还原
                policy_mine_edit_tv.setText("编辑");
                i = 0;
                //设置标记  区分哪个tab被选择了
                policy_mine_tab_car_tv.setTag(false);
                policy_mine_tab_life_tv.setTag(true);
                //底部指示条
                policy_mine_tab_car_line_tv.setVisibility(View.GONE);
                policy_mine_tab_life_line_tv.setVisibility(View.VISIBLE);

                policy_mine_tab_life_tv.setTextColor(getResources().getColor(R.color.baodanTab));
                policy_mine_tab_car_tv.setTextColor(getResources().getColor(R.color.textcolorHei));

                getApiMyOrder.uid = BaseApplication.BasePreferences.readUserId();
                getApiMyOrder.type = PrefrenceConfig.TWO;
                getApiMyOrder.starttime = start;
                getApiMyOrder.endtime = end;

                getApiMyOrder.execute(this);

                return;

        }


    }

    // 回调方法
    @Override
    public void getStringBaoDanId(final JSONArray array) {

        if (array != null) {

            new AlertDialog(context, "是否删除？") {
                @Override
                protected void onConfirm() {

                    if ((Boolean) policy_mine_tab_car_tv.getTag()) {

                        getApiMyOrderEdit.type = Integer.parseInt(PrefrenceConfig.ONE);
                        getApiMyOrderEdit.bcid = array;
                        getApiMyOrderEdit.execute(context);


                    } else {

                        getApiMyOrderEdit.type = Integer.parseInt(PrefrenceConfig.TWO);
                        getApiMyOrderEdit.alid = array;
                        getApiMyOrderEdit.execute(context);

                    }

                }

                @Override
                protected void onCancle() {
                    i = 1;
                }

            }.show();

        } else {

            new AlertDialog(context, "您未选择删除项目，是否继续删除？") {
                @Override
                protected void onConfirm() {


                }

                @Override
                protected void onCancle() {

                    //删除 变成 编辑   小圆点隐藏
                    adapter = new BaoDanAdapter(context, lists, false);
                    policy_mine_lv.setAdapter(adapter);
                    policy_mine_edit_tv.setText("编辑");

                }
            }.show();


        }


    }


}
